package com.chencs.biz.special.controller;
import com.alibaba.fastjson.JSON;
import com.chencs.biz.special.service.SpecialService;
import com.chencs.vo.biz.model.ModeBlockModel;
import com.chencs.vo.biz.special.SpecialModel;
import framework.model.ResponseOptUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;


/**
 * Created by fangzehua on 2017/7/8.
 */

@RequestMapping("/special")
@RestController
public class SpecialController {

    @Autowired
    private SpecialService specialService;

    @RequestMapping(value = "/doSpecialPage", method = RequestMethod.GET)
    public ModelAndView doSpecialPage() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("admin/biz/special/special_list");
        return modelAndView;
    }

    @RequestMapping(value = "/specialList", method = RequestMethod.GET)
    public String specialList(){
        String retStr = "";
        try {
            retStr = specialService.specialList();
        } catch (Exception e) {
            retStr = ResponseOptUtil.commonMsgError( ResponseOptUtil.RET_FAIL_MSG);
            e.printStackTrace();
        }
        return retStr;
    }

    @RequestMapping(value = "/doEditor/{id}", method = RequestMethod.GET)
    public ModelAndView doEditor(@PathVariable("id") String id , Model model) {

        SpecialModel model1 = null;
        try{
            model1 = JSON.parseObject(specialService.findSpecialById(id),SpecialModel.class);
        }catch (Exception e){
            e.printStackTrace();
        }
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("admin/biz/special/special_editor");
        model.addAttribute("model1",model1);

        return modelAndView;
    }

    @RequestMapping(value = "/delSpecial", method = RequestMethod.POST)
    public String delSpecial(@RequestBody String id) {
        String retStr = "";
        try {
            retStr = specialService.delSpecial(id);
        } catch (Exception e) {
            retStr = ResponseOptUtil.commonMsgError( ResponseOptUtil.RET_FAIL_MSG);
            e.printStackTrace();
        }
        return retStr;

    }


    @RequestMapping(value = "/product/{id}", method = RequestMethod.GET)
    public ModelAndView product(@PathVariable("id") String id , Model model) {
        ModelAndView modelAndView = new ModelAndView();
        model.addAttribute("id",id);
        modelAndView.setViewName("admin/biz/special/special_product_list");
        return modelAndView;
    }


    @RequestMapping(value = "/getSpecialProductById/{sid}", method = RequestMethod.GET)
    public String getSpecialProductById(@PathVariable("sid") String sid){
        String retStr = "";
        try {
            retStr = specialService.getSpecialProductById(sid);
        } catch (Exception e) {
            retStr = ResponseOptUtil.commonMsgError( ResponseOptUtil.RET_FAIL_MSG);
            e.printStackTrace();
        }
        return retStr;
    }


    @RequestMapping(value = "/delSpecialProductById", method = RequestMethod.POST)
    public String delSpecialProductById(@RequestBody String id){
        String retStr = "";
        try {
            retStr = specialService.delSpecialProduct(id);
        } catch (Exception e) {
            retStr = ResponseOptUtil.commonMsgError( ResponseOptUtil.RET_FAIL_MSG);
            e.printStackTrace();
        }
        return retStr;
    }

    @RequestMapping(value = "/saveSpecialProduct", method = RequestMethod.POST)
    public String saveSpecialProduct(@RequestBody String id){
        String retStr = "";
        try {
            retStr = specialService.saveSpecialProduct(id);
        } catch (Exception e) {
            retStr = ResponseOptUtil.commonMsgError( ResponseOptUtil.RET_FAIL_MSG);
            e.printStackTrace();
        }
        return retStr;
    }


    @RequestMapping(value = "/doAddSpecial", method = RequestMethod.GET)
    public ModelAndView doAddSpecial(Model model) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("admin/biz/special/special_add");
        return modelAndView;
    }

    @RequestMapping(value = "/insertSpecial", method = RequestMethod.POST)
    public String insertSpecial(@RequestBody SpecialModel model){
        String retStr = "";
        try {
            retStr = specialService.insertSpecial(model);
        } catch (Exception e) {
            retStr = ResponseOptUtil.commonMsgError( ResponseOptUtil.RET_FAIL_MSG);
            e.printStackTrace();
        }
        return retStr;
    }

    @RequestMapping(value = "/updateSpecial", method = RequestMethod.POST)
    public String updateSpecial(@RequestBody SpecialModel model){
        String retStr = "";
        try {
            retStr = specialService.updateSpecial(model);
        } catch (Exception e) {
            retStr = ResponseOptUtil.commonMsgError( ResponseOptUtil.RET_FAIL_MSG);
            e.printStackTrace();
        }
        return retStr;
    }




}
